home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 5059 < prev    next >
Encoding:
Internet Message Format  |  1996-08-05  |  4.0 KB

  1. Path: mail2news.demon.co.uk!genesis.demon.co.uk
  2. From: Lawrence Kirby <fred@genesis.demon.co.uk>
  3. Newsgroups: comp.lang.c
  4. Subject: Re: newbie's problems w/ linux
  5. Date: Mon, 12 Feb 96 20:06:33 GMT
  6. Organization: none
  7. Message-ID: <824155593snz@genesis.demon.co.uk>
  8. References: <4fnrq8$53v@news.datatamers.com>
  9. Reply-To: fred@genesis.demon.co.uk
  10. X-NNTP-Posting-Host: genesis.demon.co.uk
  11. X-Newsreader: Demon Internet Simple News v1.27
  12. X-Mail2News-Path: genesis.demon.co.uk
  13.  
  14. In article <4fnrq8$53v@news.datatamers.com> wnc@datatamers.com "
  15. " writes:
  16.  
  17. >i'm currently taking a beginners C class, and i was attempting to make a 
  18. >program which figures out GPA for my classes. I'm running windows 95, and 
  19. >when i compile it for DOS, it runs fine, but, when i compile it for 
  20. >linux, using gcc, i keep getting this same error, basically what happens 
  21. >is that after taking input through scanf(), it will skip the next 
  22. >scanf(), even though i'm using fflush(). here is a sample output of what 
  23. >happens.
  24. >
  25. >
  26. >GPA version .9beta, written by wnc@datatamers.com
  27. >
  28. >
  29. >How many classes do you have? 2
  30. >What grade did you get in period 1? a
  31. >What grade did you get in period 2? Do you have any AP classes?
  32. >
  33. >Your Total GPA for 2 period(s) is 0!
  34. >Which is equivilent to a(n) F
  35. >
  36. >
  37. >here is the source, if you can figure out what's wrong w/ it, please 
  38. >mail me at wnc@datatamers.com, thanks in advance.
  39. >
  40. >#include <stdio.h>
  41. >void convert(void);
  42. >void endo(void);
  43. >float converted, gpa = 0., ap, donegpa;
  44. >int periods;
  45. >char grade, endofit[2], yesno;
  46. >void main()
  47.  
  48. In C main returns an int.
  49.  
  50. >{
  51. >short int counter, counter2;
  52.  
  53. int is typically mapped to the most efficient type the archetecture
  54. can handle therefore you should use it for auxiliary variables unless
  55. you need something longer. Shorts have very little use other than in
  56. structures and arrays.
  57.  
  58. >printf("\n\n\n\n\n\n\nGPA version .9beta, written by
  59. > wnc@datatamers.com\n\n\n"); 
  60. >printf("How many classes do you have? ");
  61. >scanf("%i", &periods);
  62.  
  63. Look up the difference between the %i and %d scanf format specifiers. I'm
  64. not saying %i is necessarily wrong here (although it may be if you
  65. aren't using an ANSI compiler/library), just that %d more commonly
  66. corresponds to what was wanted.
  67.  
  68. >fflush(stdin);
  69.  
  70. This results in undefined behaviour. fflush() is only defined on output
  71. streams or update streams where the last operation was a write. You can
  72. also pass NULL to fflush (many pre-ANSI libraries didn't support this
  73. though.
  74.  
  75. >for ( counter = 1; counter <= periods; ++counter )
  76. >{
  77. >printf("What grade did you get in period %i? ", counter);
  78.  
  79. %d and %i means the same thing in printf formats. However %d has been
  80. supported much longer by C compilers/libraries and is more commonly used.
  81.  
  82. Here is where you may need a fflush(). The C runtime system may have set
  83. stdout as line buffered here in which case it won't write anything until
  84. the program writes a newline to stdout. Alternatively you can force buffered
  85. output to be written using:
  86.  
  87. fflush(stdout);
  88.  
  89. >scanf("%d", &grade);
  90.  
  91. Here you are passing a pointer to a char to a format specifier which
  92. requires a pointer to an int. GCC should have warned you about this
  93. if you used an adequate warning level (-Wall will probably do it).
  94.  
  95. >fflush(stdin);
  96. >convert();
  97. >gpa += converted;
  98. >}
  99. >printf("Do you have any AP classes? ");
  100. >scanf("%d",& yesno);
  101.  
  102. Again you are trying to read an int value into a char and passing the
  103. wring type of pointer which is illegal. It looks from below that you were
  104. trying to read a charater in which case you could use:
  105.  
  106. scanf(" %c",& yesno);
  107.  
  108. Note the space is important since it skips over leading white-space in the
  109. input data (which %c doesn't do automatically).
  110.  
  111. A generally better approach for getting input is to read in whole lines
  112. using fgets() into a buffer and then scanning them with sscanf, atoi,
  113. strtok etc. scanf by itself isn't too hot at dealing with line based
  114. input.
  115.  
  116. -- 
  117. -----------------------------------------
  118. Lawrence Kirby | fred@genesis.demon.co.uk
  119. Wilts, England | 70734.126@compuserve.com
  120. -----------------------------------------
  121.